﻿namespace Chain.DAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class CouponList
    {
        public int Add(Chain.Model.CouponList model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into CouponList(");
            builder.Append("CouPonID,CouPon,CouPonYF,CouPonSY)");
            builder.Append(" values (");
            builder.Append("@CouPonID,@CouPon,@CouPonYF,@CouPonSY)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CouPonID", SqlDbType.Int, 4), new SqlParameter("@CouPon", SqlDbType.NVarChar, 50), new SqlParameter("@CouPonYF", SqlDbType.Bit, 1), new SqlParameter("@CouPonSY", SqlDbType.Bit, 1) };
            cmdParms[0].Value = model.CouPonID;
            cmdParms[1].Value = model.CouPon;
            cmdParms[2].Value = false;
            cmdParms[3].Value = false;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public Chain.Model.CouponList DataRowToModel(DataRow row)
        {
            Chain.Model.CouponList list = new Chain.Model.CouponList();
            if (row != null)
            {
                if ((row["CID"] != null) && (row["CID"].ToString() != ""))
                {
                    list.CID = int.Parse(row["CID"].ToString());
                }
                if ((row["CouPonID"] != null) && (row["CouPonID"].ToString() != ""))
                {
                    list.CouPonID = int.Parse(row["CouPonID"].ToString());
                }
                if (row["CouPon"] != null)
                {
                    list.CouPon = row["CouPon"].ToString();
                }
                if ((row["CouPonYF"] != null) && (row["CouPonYF"].ToString() != ""))
                {
                    if ((row["CouPonYF"].ToString() == "1") || (row["CouPonYF"].ToString().ToLower() == "true"))
                    {
                        list.CouPonYF = true;
                    }
                    else
                    {
                        list.CouPonYF = false;
                    }
                }
                if ((row["CouPonSY"] != null) && (row["CouPonSY"].ToString() != ""))
                {
                    if ((row["CouPonSY"].ToString() == "1") || (row["CouPonSY"].ToString().ToLower() == "true"))
                    {
                        list.CouPonSY = true;
                    }
                    else
                    {
                        list.CouPonSY = false;
                    }
                }
                if ((row["CouPonMID"] != null) && (row["CouPonMID"].ToString() != ""))
                {
                    list.CouPonMID = int.Parse(row["CouPonMID"].ToString());
                }
                if ((row["ConPonSendTime"] != null) && (row["ConPonSendTime"].ToString() != ""))
                {
                    list.ConPonSendTime = DateTime.Parse(row["ConPonSendTime"].ToString());
                }
                if ((row["ConPonUseTime"] != null) && (row["ConPonUseTime"].ToString() != ""))
                {
                    list.ConPonUseTime = DateTime.Parse(row["ConPonUseTime"].ToString());
                }
                if (row["CouPonOrderAccount"] != null)
                {
                    list.CouPonOrderAccount = row["CouPonOrderAccount"].ToString();
                }
            }
            return list;
        }

        public bool DataUpdateTran(ArrayList sqlArray)
        {
            return DbHelperSQL.ExecuteSqlTran(sqlArray);
        }

        public bool Delete(int CID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from CouponList ");
            builder.Append(" where CID=@CID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string CIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from CouponList ");
            builder.Append(" where CID in (" + CIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int CID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from CouponList");
            builder.Append(" where CID=@CID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetCouponDetail(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select CID,CouPonID,CouPon,CouponList.CouPonYF,CouponList.CouPonSY,CouPonMID,ConPonSendTime,ConPonUseTime,CouPonOrderAccount");
            builder.Append(",ID,CouponTitle,CouponType,CouponNumber,CouponPredictNu,CouponEffective,CouponStart,CouponEnd,CouponDayNum,CouponMinMoney,CouponContent,MemName ");
            builder.Append(" FROM CouponList,Coupon,Mem ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where Mem.MemID=CouponList.CouPonMID and " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select CID,CouPonID,CouPon,CouPonYF,CouPonSY,CouPonMID,ConPonSendTime,ConPonUseTime,CouPonOrderAccount ");
            builder.Append(" FROM CouponList ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" CID,CouPonID,CouPon,CouPonYF,CouPonSY,CouPonMID,ConPonSendTime,ConPonUseTime,CouPonOrderAccount ");
            builder.Append(" FROM CouponList ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, string[] strWhere, out int resCount)
        {
            string tableName = " V_CouponList ";
            string[] column = new string[] { "* " };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "CID", true, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("CID", "CouponList");
        }

        public DataSet GetMemInfoListSP(int PageSize, int PageIndex, string[] strWhere, out int resCount)
        {
            string tableName = " CouponList,Mem ";
            string[] column = new string[] { " CouponList.* " };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "CID", true, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public Chain.Model.CouponList GetModel(string CouPon)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 * from CouponList ");
            builder.Append(" where CouPon=@CouPon");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CouPon", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = CouPon;
            new Chain.Model.CouponList();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                return this.DataRowToModel(set.Tables[0].Rows[0]);
            }
            return null;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM CouponList ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.CouponList model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update CouponList set ");
            builder.Append("CouPonID=@CouPonID,");
            builder.Append("CouPonYF=@CouPonYF,");
            builder.Append("CouPonSY=@CouPonSY,");
            builder.Append("CouPonMID=@CouPonMID,");
            builder.Append("ConPonUseTime=@ConPonUseTime,");
            builder.Append("CouPonOrderAccount=@CouPonOrderAccount ");
            builder.Append(" where CID=@CID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CouPonID", SqlDbType.Int, 4), new SqlParameter("@CouPonYF", SqlDbType.Bit, 1), new SqlParameter("@CouPonSY", SqlDbType.Bit, 1), new SqlParameter("@CouPonMID", SqlDbType.Int, 4), new SqlParameter("@ConPonUseTime", SqlDbType.DateTime), new SqlParameter("@CouPonOrderAccount", SqlDbType.VarChar, 50), new SqlParameter("@CID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.CouPonID;
            cmdParms[1].Value = model.CouPonYF;
            cmdParms[2].Value = model.CouPonSY;
            cmdParms[3].Value = model.CouPonMID;
            cmdParms[4].Value = model.ConPonUseTime;
            cmdParms[5].Value = model.CouPonOrderAccount;
            cmdParms[6].Value = model.CID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

